package com.playball.lottery.customview;

import java.util.List;
import java.util.Map;

import com.playball.lottery.R;
import com.playball.lottery.frm.buyhall.IntelligenceChaseActivity;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
 * 智能追号的适配器
* @ClassName: IntelligenceChaseAdapter 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author 2w 
* @date 2014年7月22日 下午8:54:47 
*
 */
public class IntelligenceChaseAdapter extends BaseAdapter {

	/** 上下文 */
	private Context context;
	/** LayoutInflater */
	private LayoutInflater inflater = null;
	
	private List<Map<String, Object>> dataList;
	
	/**最大 倍数*/
	private int issueMaxMul = 999 ;
	
	public IntelligenceChaseAdapter(Context context) {
		super();
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	public   void setData(List<Map<String, Object>> dataList) {
		this.dataList = dataList ;
	}
	
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView=	inflater.inflate(R.layout.buy_intelligence_chase_list_item, null);
		}
		//序号
		TextView txt_intell_No = (TextView) convertView.findViewById(R.id.txt_intell_No);
		int Num = position + 1 ; 
		txt_intell_No.setText(Integer.toString(Num));
		
		//期次
		TextView txt_intell_issue = (TextView) convertView.findViewById(R.id.txt_intell_issue);
		txt_intell_issue.setText(dataList.get(position).get("issueName").toString());
		
		//倍数
		final EditText edt_intell_input_mul = (EditText) convertView.findViewById(R.id.edt_intell_input_mul);
		edt_intell_input_mul.setText(Integer.parseInt(dataList.get(position).get("castMutiple").toString())  + "" );
		
		//减
		Button btnSub = (Button) convertView.findViewById(R.id.btn_intell_sub);
		btnSub.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int intInputMul = Integer.parseInt(edt_intell_input_mul.getText().toString().trim())	 ;
				if (intInputMul < 1) {
					edt_intell_input_mul.setText("1");
				}else {
					intInputMul -- ;
					if (intInputMul == 0) {
						edt_intell_input_mul.setText("1");
					}else {
						Log.d("van", intInputMul+"");
						( (IntelligenceChaseActivity)context).notifyData(intInputMul,position ,2);
					}
				}
				
				
			}
		});
		
		
		//加
		Button btnAdd = (Button) convertView.findViewById(R.id.btn_intell_add);
		btnAdd.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int intInputMul = Integer.parseInt(edt_intell_input_mul.getText().toString().trim())	 ;
				if (intInputMul > issueMaxMul) {
					edt_intell_input_mul.setText(issueMaxMul+"");
				}else {
					intInputMul ++ ;
					if (intInputMul == issueMaxMul) {
						edt_intell_input_mul.setText(issueMaxMul+"");
					}else {
						Log.d("van", intInputMul+",,,,," + position);
						( (IntelligenceChaseActivity)context).notifyData(intInputMul,position,1);
					}
				}
				
				
			}
		});
		
		
		
		//累计投入
		TextView txt_intell_addup_input = (TextView) convertView.findViewById(R.id.txt_intell_addup_input);
		txt_intell_addup_input.setText(dataList.get(position).get("totalCost").toString());
		
		//中奖盈利
		TextView txt_intell_win_profit = (TextView) convertView.findViewById(R.id.txt_intell_win_profit);
		txt_intell_win_profit.setText(	(int) Double.parseDouble((dataList.get(position).get("totalIncome").toString())) +"");
		
		//盈利率
		TextView txt_intell_profit_rate = (TextView) convertView.findViewById(R.id.txt_intell_profit_rate);
		txt_intell_profit_rate.setText(String.format("%.2f", dataList.get(position).get("incomeRate"))+"%");
		
		return convertView ;
	}

}
